package com.example.myapplication;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

import androidx.annotation.NonNull;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class RateListActivity extends ListActivity {
    private static final String TAG = "RateList";


    Handler handler;
    @Override
    protected void onCreate(Bundle saveInstanceAtate){
        super.onCreate(saveInstanceAtate);

        List<String> list =new ArrayList<String>();
        for(int i =10;i<50;i++){
            list.add("Item"+i);
        }


        String[] list_data ={"one","two","three","four"};
        ListAdapter adapter = new ArrayAdapter<String >(this, android.R.layout.simple_list_item_1,list_data);
        setListAdapter(adapter);

        handler =new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(@NonNull Message msg){
                Log.i(TAG, "handleMessage: 获取返回的数据项");
                if(msg.what==7){
                    List<String> list2 = (List<String>) msg.obj;
                    ListAdapter adapter = new ArrayAdapter<String >(RateListActivity.this, android.R.layout.simple_list_item_1,list2);
                    setListAdapter(adapter);
                }
                super.handleMessage(msg);
            }

        };

        //开启线程获取数据
        new Thread(()->{
            //线程运行代码
            List<String> retList = new ArrayList<String>();
            try {
                Thread.sleep(3000);

                Document doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();
                Log.i(TAG, "run: Title=" + doc.title());
                Elements tables = doc.getElementsByTag("table");
                Element table = tables.get(1);
                Elements trs = table.getElementsByTag("tr");

                trs.remove(0);
                for (Element tr : trs) {
                    Elements tds = tr.children();
                    Element td1 =tds.get(0);
                    Element td2 =tds.get(5);
                    String str1 =td1.text();
                    String str2 =td2.text();
                    Log.i(TAG, "run: "+str1+"==>"+str2);
                    float r = 100f/Float.parseFloat(str2);
                    retList.add(str1+"==>"+str2);
                }

            } catch (InterruptedException e) {
                Log.e(TAG, "onCreate: 执行出错:" + e );
            } catch (IOException e) {
                Log.e(TAG, "onCreate: 执行出错:" + e );
            }

            Log.i(TAG, "onCreate: 返回数据");
            //带回数据
            Message msg = handler.obtainMessage(7,retList);
            handler.sendMessage(msg);



        }).start();
    }
}



